package com.ruoyi.web.controller.api;


import com.ruoyi.cat.domain.CatBannerTable;
import com.ruoyi.cat.domain.CatUserbasicTable;
import com.ruoyi.cat.domain.DTO.AmountMoneyDTO;
import com.ruoyi.cat.domain.DTO.TotalAmountDTO;
import com.ruoyi.cat.domain.DTO.TradedetailTableDTO;
import com.ruoyi.cat.domain.VO.TradedetailTableVO;
import com.ruoyi.cat.service.ICatTradedetailTableService;
import com.ruoyi.cat.service.ICatUserbasicTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.WxTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;


@Api(tags = "api-交易记录")
@RestController
@RequestMapping("/api/transaction")
public class TransactionController extends BaseController {


    @Autowired
    private ICatTradedetailTableService catTradedetailTableService;


    @Autowired
    private WxTokenService wxTokenService;

    @Autowired
    private ICatUserbasicTableService catUserbasicTableService;


    @ApiOperation("api-获取交易记录")
    @GetMapping("/getTransaction")
    public TableDataInfo list(HttpServletRequest request, TradedetailTableVO tradedetailTableVO)
    {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        tradedetailTableVO.setUserId(wxLoginUser.getUserId());
        startPage();
        List<TradedetailTableDTO> list = catTradedetailTableService.selectCatTradedetailTableApiList(tradedetailTableVO);
        return getDataTable(list);
    }


    @ApiOperation("api-交易金额")
    @GetMapping("/getAmountMoney")
    public AjaxResult getAmountMoney(HttpServletRequest request)
    {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        AmountMoneyDTO amountMoneyDTO = new AmountMoneyDTO();
        TotalAmountDTO totalAmountDTO = catTradedetailTableService.getTotalAmout(wxLoginUser.getUserId());
        CatUserbasicTable catUserbasicTable = catUserbasicTableService.selectCatUserbasicTableById(wxLoginUser.getUserId());
        amountMoneyDTO.setAmountMoney(catUserbasicTable.getAmount());
        if (totalAmountDTO == null){
            amountMoneyDTO.setDayAmountMoney(new BigDecimal(0));
        }else {
            amountMoneyDTO.setDayAmountMoney(totalAmountDTO.getTotalAmount());
        }
       return success(amountMoneyDTO);
    }

}
